package com.liugai.lgeachday.request;

import com.liugai.tool.string.StringUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class RequestCheck {

    public static Response checkEmptyParam(Object obj) {
        if (obj == null) {
            return new Response(200101, null, null);
        }
        return null;
    }

    public static Response checkMustParam(Object obj, List<String> mustParams) {
        if (mustParams != null) {
            Response response = checkEmptyParam(obj);
            if (response != null) {
                return response;
            }
            boolean isPassed = true;
            List<Method> allMethods = List.of(obj.getClass().getMethods());
            for (String param: mustParams) {
                String pMathod = "get"+ StringUtil.toUpperCaseFirstOne(param);
                if (allMethods.contains(pMathod)) {
                    Method method = allMethods.get(allMethods.indexOf(pMathod));
                    try {
                        Object res = method.invoke(obj);
                        if (res == null) {
                            isPassed = false;
                            break;
                        }

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (!isPassed) {
                return Response.unMatchParamResponse(null);
            }
        }
        return null;
    }
}
